import wx
import wx.lib.mixins.listctrl as listmix
from wx.lib.scrolledpanel import ScrolledPanel
import wx.lib.buttons as buttons


class TreeCombo(wx.Window):
    def __init__(self, parent, id=-1, value="", choices=[], pos=wx.DefaultPosition, size=(160, -1), style=0):
        super(TreeCombo, self).__init__(parent, id, pos, size, style)
        self.value = value
        self.choices = choices
        self.parent = parent

        # 创建一个文本框用于显示当前选择
        self.text_ctrl = wx.TextCtrl(self, -1, value, style=wx.TE_READONLY)
        self.text_ctrl.SetMinSize((size[0], 20))

        # 创建一个按钮用于打开下拉框
        self.button = buttons.GenBitmapButton(self, -1,
                                              wx.ArtProvider.GetBitmap(wx.ART_LIST_VIEW, wx.ART_TOOLBAR, (16, 16)),
                                              size=(20, 20))
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

        # 创建下拉框窗口（初始时隐藏）
        self.dropdown = wx.PopupWindow(self, wx.SIMPLE_BORDER)
        self.dropdown_panel = ScrolledPanel(self.dropdown)
        self.dropdown_panel.SetScrollbars(1, 1, 100, len(choices) * 20)

        # 创建树形控件
        self.tree = wx.TreeCtrl(self.dropdown_panel, style=wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT)

        # 填充树形控件的节点
        self.root = self.tree.AddRoot("Root")
        for item in choices:
            child = self.tree.AppendItem(self.root, item["label"])
            if "children" in item:
                for sub_item in item["children"]:


                    self.tree.AppendItem(child, sub_item["label"])

                    # 绑定树形控件的事件
            self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeSelected)

        # 布局
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.text_ctrl, proportion=1, flag=wx.ALL | wx.EXPAND, border=2)
        sizer.Add(self.button, flag=wx.ALL | wx.EXPAND, border=2)
        self.SetSizer(sizer)


    def OnButton(self, event):
        # 显示下拉框
        self.dropdown.SetSize(300, 200)
        pos = self.GetScreenRect().GetBottomLeft()
        self.dropdown.Position(pos, (0, 0))
        self.dropdown.Show()


    def OnTreeSelected(self, event):
        # 当选择树形控件的节点时，更新文本框的值并隐藏下拉框
        item = event.GetItem()
        self.text_ctrl.SetValue(self.tree.GetItemText(item))
        self.dropdown.Hide()


    def SetValue(self, value):
        self.value = value
        self.text_ctrl.SetValue(value)


    def GetValue(self):
        return self.text_ctrl.GetValue()


# 使用示例
class MyFrame(wx.Frame):
    def __init__(self, parent):
        super(MyFrame, self).__init__(parent, title="Tree Combo Example")

        choices = [
            {"label": "Parent 1", "children": [{"label": "Child 1.1"}, {"label": "Child 1.2"}]},
            {"label": "Parent 2", "children": [{"label": "Child 2.1"}, {"label": "Child 2.2"}]},
        ]

        combo = TreeCombo(self, choices=choices)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(combo, proportion=0, flag=wx.ALL | wx.EXPAND, border=10)
        self.SetSizer(sizer)


app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()